﻿using System;
using System.Linq;
using System.Web.Mvc;

namespace Infrastructure.Mvc.SeedWork.Extensions
{
    public static class UrlHelperExtensions
    {
        public static string ActionForArea(this UrlHelper urlHelper, string actionName, string controllerName,
            string areaName)
        {
            return urlHelper.Action(actionName, controllerName, new {area = areaName});
        }

        public static string Action(this UrlHelper urlHelper, string actionName, string controllerName,
            string areaName, long id)
        {
            return urlHelper.Action(actionName, controllerName, new {area = areaName, Id = id});
        }

        public static string ActionForUrl(this UrlHelper urlHelper, string actionUrl)
        {
            string[] arr = actionUrl.Split('/').Where(x => x != "").ToArray();
            if (arr.Length == 3)
            {
                string actionName = arr[2];
                string controllerName = arr[1];
                string areaName = arr[0];
                return urlHelper.Action(actionName, controllerName, new {area = areaName});
            }
            if (arr.Length == 2)
            {
                string actionName = arr[1];
                string controllerName = arr[0];
                return urlHelper.Action(actionName, controllerName);
            }
            if (arr.Length == 1)
            {
                string actionName = arr[0];
                return urlHelper.Action(actionName);
            }
            throw new Exception(@"Url传值格式不正确！");
        }
    }
}